package car.slyc.controller;
import car.slyc.model.User;
import car.slyc.service.UserServiceImpl;
import car.slyc.util.JsonObject;
import car.slyc.util.PageRet;
import car.slyc.util.Time;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/user")
public class UserController {


    @Resource
    private UserServiceImpl service;

//--------------------------------------------小程序-------------------------------------

    //登录-----前端
    @RequestMapping("/qt/login")
    @ResponseBody
    public JsonObject login(@RequestParam("username") String username,
                            @RequestParam("pass") String pass,
                            @RequestParam("type") String type,
                            HttpSession session) {
        System.out.println(username);
        System.out.println(pass);
        System.out.println(type);
        User u = service.getByUsernametype(username,type);
        if (u != null) {
            if (u.getPass().equals(pass)) {
                session.setAttribute("user", u);
                if (u.getState().equals("通过审核")){
                    return new JsonObject("200", "登录成功", u);
                }else if (u.getState().equals("未通过审核")){
                    return new JsonObject("100", "未通过审核", u);
                } else{
                    return new JsonObject("400", "审核中", u);
                }
            } else {
                return new JsonObject("300", "密码错误", null);
            }
        } else {
            return new JsonObject("500", "无此用户", null);
        }

    }


    //    注册--- 前端
    @RequestMapping("/qt/regist")
    @ResponseBody
    public JsonObject regist(User user, Model model, HttpSession session) {
        User u = service.getByUsername(user.getUsername());
        try {
            if (u == null) {
                user.setId(UUID.randomUUID().toString().replace("-", ""));
                user.setTime(Time.getDate());
                service.add(user);
                return new JsonObject("200", "注册成功", null);
            } else {
                return new JsonObject("300", "用户名已存在", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.toString());
            return new JsonObject("500", "注册失败", null);
        }
    }






    //   跳转 显示
    @RequestMapping("/tolist")
    public String tolist() {

        return "User/list";
    }

    //   跳转 添加
    @RequestMapping("/toadd")
    public String toadd(Model model) {

        model.addAttribute("modyid", "");

        return "User/add";
    }

    //   跳转 修改
    @RequestMapping("/toget")
    public String toget(String id, Model model) {
        System.out.println("修改的数据" + id);
        model.addAttribute("modyid", id);
        return "User/add";
    }




    //   跳转司机列表 显示
    @RequestMapping("/tolistsj")
    public String tolistsj() {

        return "User/listsj";


    }

    //   跳转司机 添加 添加
    @RequestMapping("/toaddsj")
    public String toaddsj(Model model) {

        model.addAttribute("modyid", "");

        return "User/addsj";
    }

    //   跳转 修改
    @RequestMapping("/togetsj")
    public String togetsj(String id, Model model) {
        System.out.println("修改的数据" + id);
        model.addAttribute("modyid", id);
        return "User/addsj";
    }





    // 跳转详情页
    @RequestMapping("/tolistxq")
    public ModelAndView tolistxq(@RequestParam("tid") String tid, ModelAndView modelAndView) {
        System.out.println("显示详情页面tid：" + tid);
        modelAndView.addObject("info", service.getById(tid));
        modelAndView.setViewName("User/listxq");
        return modelAndView;

    }


    //   添加
    @RequestMapping("/add")
    @ResponseBody
    public PageRet add(User user, Model model) {

        System.out.println(Time.getDate());
        User u = service.getByUsername(user.getUsername());
        try {
            if (u == null) {
                user.setId(UUID.randomUUID().toString().replace("-", ""));
                user.setTime(Time.getDate());
                service.add(user);
                return new PageRet(200, "添加成功", null, 0);
            } else {
                return new PageRet(300, "用户已存在", null, 0);
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.toString());
            return new PageRet(500, "用户已存在", null, 0);
        }

    }

    //   修改
    @RequestMapping("/get")
    @ResponseBody
    public JsonObject getById(String id) {

        User user = service.getById(id);
        System.out.println(user);

        return new JsonObject("200", "", user);
    }


    //   修改
    @RequestMapping("/update")
    @ResponseBody
    public PageRet update(User user) {
        System.out.println("前端传输的数据：" + user);
        if (service.update(user) > 0) {
            return new PageRet(200, "", null, 0);
        } else {
            return new PageRet(500, "", null, 0);
        }
    }

    //   修改
    @RequestMapping("/update_state")
    @ResponseBody
    public PageRet update_state(@RequestParam("id") String id, @RequestParam("state") String state) {

        System.out.println("id=：" + id + "&state=" + state);
        if (service.update_state(id, state) > 0) {
            return new PageRet(200, "", null, 0);
        } else {
            return new PageRet(500, "", null, 0);
        }
    }

    //   修改 审核状态
    @RequestMapping("/update_shzt")
    @ResponseBody
    public PageRet update_shzt(@RequestParam("id") String id, @RequestParam("shzt") String shzt) {

        System.out.println("id=：" + id + "&shzt=" + shzt);
        if (service.update_shzt(id, shzt) > 0) {
            return new PageRet(200, "", null, 0);
        } else {
            return new PageRet(500, "", null, 0);
        }
    }





    //   批量删除
    @RequestMapping("/del")
    @ResponseBody
    public PageRet dels(@RequestParam("id") String ids) {

        System.out.println("传递过来的数据：" + ids.toString());
        if (service.dels(ids) > 0) {
            return new PageRet(200, "成功", null, 0);
        } else {
            return new PageRet(500, "失败", null, 0);
        }
    }


    //   显示所有   分页
    @RequestMapping(value = "/pagegetallck")
    @ResponseBody
    public PageRet listck(@RequestParam(value = "page", defaultValue = "0") int page,
                        @RequestParam(value = "limit", defaultValue = "2") int limit,
                        @RequestParam(value = "name", defaultValue = "0") String name) {

        //  查询之前调用    传入 当前页码、每页条数  及排序方法
        PageHelper.startPage(page, limit, " time desc");

        //startPage 后面紧跟的查询是分页查询
        List<User> list = service.getallBytype(name,"乘客");

        //用PageInfo 对结果进行包装，传入连续显示的页数
        PageInfo<User> all = new PageInfo<>(list);

        System.out.println("pageinfo包装后的数据" + all.getList());
        return new PageRet(0, "", all.getList(), (int) all.getTotal());
    }




    //   显示所有   分页
    @RequestMapping(value = "/pagegetallsj")
    @ResponseBody
    public PageRet listsj(@RequestParam(value = "page", defaultValue = "0") int page,
                        @RequestParam(value = "limit", defaultValue = "2") int limit,
                        @RequestParam(value = "name", defaultValue = "0") String name) {

        //  查询之前调用    传入 当前页码、每页条数  及排序方法
        PageHelper.startPage(page, limit, " time desc");

        //startPage 后面紧跟的查询是分页查询
        List<User> list = service.getallBytype(name,"司机");

        //用PageInfo 对结果进行包装，传入连续显示的页数
        PageInfo<User> all = new PageInfo<>(list);

        System.out.println("pageinfo包装后的数据" + all.getList());
        return new PageRet(0, "", all.getList(), (int) all.getTotal());
    }


    @RequestMapping("/cx")
    @ResponseBody
    public JsonObject cx() {
        return new JsonObject("200", "", service.getall("66"));

    }


}
